<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="项目框架号" prop="code">
        <el-input v-model="queryParams.code" placeholder="请输入项目框架号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="合同编号" prop="contractCode">
        <el-input v-model="queryParams.contractCode" placeholder="请输入合同编号" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input v-model="queryParams.customName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务员" prop="salesmanName">
        <el-input v-model="queryParams.salesmanName" placeholder="请输入业务员" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="立项日期" prop="contractDate">
        <el-date-picker clearable v-model="queryParams.contractDate" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择立项日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['develop:Project:add']">新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
      :summary-method="getSummaries" show-summary :max-height="tableMaxHeight" @sort-change="handleSortChange">
      <el-table-column type="selection" width="60" align="center" />
      <el-table-column label="框架号" align="center" prop="code" :resizable="true" />
      <el-table-column label="合同编号" align="center" prop="contractCode" :resizable="true" />
      <el-table-column label="框架名称" align="center" prop="name" show-overflow-tooltip :resizable="true" />
      <el-table-column align="center" label="项目来源" prop="source" :resizable="true">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.project_source" :value="scope.row.source" />
        </template>
      </el-table-column>

      <el-table-column  label="客户名称" align="center" prop="customName" show-overflow-tooltip :resizable="true"/>
      <el-table-column sortable align="center" label="制单人" prop="createBy" />
      <el-table-column  label="业务员" align="center" prop="salesmanName" width="100" show-overflow-tooltip :resizable="true" />
      <el-table-column  label="签约日期" align="center" prop="contractDate" width="100" :resizable="true">

        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="contractMoney" width="100" :resizable="true" />
      <el-table-column label="中标服务费" align="center" prop="bidServiceMoney" width="100" :resizable="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button @click="xiaoshoudan(scope.row)" icon="el-icon-view" size="mini" type="text">
            销售订单
          </el-button>


          <el-popover placement="top" width="200">
            <div style=" margin: 0">
              <el-button @click="caigoudanChanpin(scope.row)" icon="el-icon-view" size="mini" type="text">
                采购单产品
              </el-button>
              <!--<el-button @click="paichanjihua(scope.row)" icon="el-icon-files" size="mini" type="text">
            生产排产计划
          </el-button>
          <el-button @click="shengchandingdan(scope.row)" icon="el-icon-document" size="mini" type="text">
            生产订单
          </el-button>
          <el-button @click="shengchangongdan(scope.row)" icon="el-icon-reading" size="mini" type="text">
            生产工单
          </el-button>-->
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                v-hasPermi="['develop:Project:edit']">修改
              </el-button>
              <el-popconfirm :title="`确认删除当前数据项？`" @confirm="handleDelete(scope.row)" style="margin-left: 10px;">
                <el-button slot="reference" size="mini" type="text" icon="el-icon-delete"
                  v-hasPermi="['develop:Project:remove']">删除
                </el-button>
              </el-popconfirm>
            </div>
            <el-button slot="reference" size="mini" type="text" icon="el-icon-d-arrow-right">更多</el-button>
          </el-popover>

        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <caigoudanChanpinForm-List ref="caigoudanChanpinForm"></caigoudanChanpinForm-List>
    <paichanjihuaForm-List ref="paichanjihuaForm"></paichanjihuaForm-List>
    <shengchandingdanForm-List ref="shengchandingdanForm"></shengchandingdanForm-List>
    <shengchangongdanForm-List ref="shengchangongdanForm"></shengchangongdanForm-List>
    <xiaoshoudanForm-List ref="xiaoshoudanForm"></xiaoshoudanForm-List>
    <project-form ref="form" @ok="getList"></project-form>
  </div>
</template>

<script>
import { accAdd, deleteAction, getAction } from '@/api/manage'
import ProjectForm from '@/views/develop/project/module/ProjectForm'
import CaigoudanChanpinFormList from '@/views/develop/projectBaseInfo/prjForGoodsIn/CaigoudanChanpinFormList'
import PaichanjihuaFormList from '@/views/develop/factory/factoryProducePlan/PaichanjihuaFormList'
import ShengchandingdanFormList from '@/views/develop/factory/factoryProduceMain/ShengchandingdanFormList'
import ShengchangongdanFormList from '@/views/develop/factory/factoryProduceWork/ShengchangongdanFormList'
import XiaoshoudanFormList from '@/views/develop/projectBaseInfo/prjForGoodsIn/XiaoshoudanFormList'

export default {
  name: "ProjectList",
  dicts: ["project_source"],
  components: {
    ProjectForm,
    CaigoudanChanpinFormList,
    PaichanjihuaFormList,
    ShengchandingdanFormList,
    ShengchangongdanFormList,
    XiaoshoudanFormList
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目信息管理表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        deptName: null,
        name: null,
        code: null,
        contractCode: null,
        salesmanId: null, // 业务员
        bidServiceMoney: null,
        customId: null,
        customName: null,
        customSimple: null,
        companyName: null,
        contractDate: null,
        contractMoney: null,
        fundCallback: null,
        backMoney: null,
        remainMoney: null,
        feeMoney: null,
        saleMoney: null,
        buyMoney: null,
        profitMoney: null,
        taxStatus: null,
        status: null,
        salesmanName: null,
        orderByColumn: "create_time",
        isAsc: "desc"
      },
      url: {
        list: '/project/project/list',
        remove: '/project/project/',
        export: '/project/project/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.handleQuery();
    },
    paichanjihua(row) {
      this.$refs.paichanjihuaForm.show(row);
    },
    shengchandingdan(row) {
      this.$refs.shengchandingdanForm.show(row);
    },
    shengchangongdan(row) {
      this.$refs.shengchangongdanForm.showByProject(row);
    },
    caigoudanChanpin(row) {
      this.$refs.caigoudanChanpinForm.shedingGygx(row);
    },
    xiaoshoudan(row) {
      this.$refs.xiaoshoudanForm.show(row);
    },
    /** 查询项目信息管理列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `project_${new Date().getTime()}.xlsx`)
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计amount1和amount3，想合并哪些列就在这里指定
        if (column.property === "contractMoney") {

          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return accAdd(prev, curr);
            } else {
              return prev;
            }
          }, 0);
        }
      });
      return sums;
    },




  }
};
</script>
